package com.fanpan26.algo.sort.practice;

import java.util.Arrays;

/**
 * @author fanpan26
 */
public abstract class AbstractDaySort {

    public final void run() {
        int[] bubbleArray = new int[]{4, 5, 3, 2, 6, 5, 7, 9, 1};
        int[] insertionArray = new int[]{4, 5, 3, 2, 6, 5, 7, 9, 1};
        int[] selectionArray = new int[]{4, 5, 3, 2, 6, 5, 7, 9, 1};
        int[] bucketArray = new int[]{4, 5, 3, 2, 6, 5, 7, 9, 1};
        int[] baseArray = new int[]{4, 5, 3, 2, 6, 5, 7, 9, 1};
        int[] countingArray = new int[]{4, 5, 3, 2, 6, 5, 7, 9, 1};
        int[] quickArray = new int[]{4, 5, 3, 2, 6, 5, 7, 9, 1};


        runInternal(bubbleArray, array -> sortByBubble(array), "冒泡排序");
        runInternal(insertionArray, array -> sortByInsertion(array), "插入排序");
        runInternal(selectionArray, array -> sortBySelection(array), "选择排序");
        runInternal(bucketArray, array -> sortByBucket(array), "桶排序");
        runInternal(baseArray, array -> sortByBase(array), "基数排序");
        runInternal(countingArray, array -> sortByCounting(array), "计数排序");
        runInternal(quickArray, array -> quickSort(array), "快速排序");

    }

    private void runInternal(int[] sortArray, SortCallable sortCallable, String type) {

        int[] result = sortCallable.sort(sortArray);

        System.out.println(type + "的运行结果为：" + Arrays.toString(result));
    }

    /**
     * 冒泡排序
     * 相邻的挨个比较，并交换
     * */
    protected abstract int[] sortByBubble(int[] arr);

    /**
     * 插入排序
     * 分为已排序部分和未排序部分
     * */
    protected abstract int[] sortByInsertion(int[] arr);

    /**
     * 选择排序
     * 选择最小的和当前索引交换
     * */
    protected abstract int[] sortBySelection(int[] arr);

    /**
     * 桶排序
     * */
    protected abstract int[] sortByBucket(int[] arr);

    /**
     * 基数排序
     * */
    protected abstract int[] sortByBase(int[] arr);

    /**
     * 计数排序
     * */
    protected abstract int[] sortByCounting(int[] arr);

    /**
     * 快速排序
     * */
    protected abstract int[] quickSort(int[] arr);

}
